import request from '@/utils/request';
import { stringify } from 'qs';
import { getPageSize } from '@/utils/common';

/**
 * 查询帐单列表
 */
export async function selectBillList(params) {
  params.pageSize = params.pageSize || getPageSize();
  return request(`/server/api/tms/tms-bill/selectTmsBillList`, {
    method: 'POST',
    body: params,
  });
}

/**
 * 查询帐单详情
 */
export async function selectBillDetail(params) {
  const { type, ...body } = params;
  const url = type === 'open' ? 'open/oaViewTmsBillDetails_s' : 'tms-bill/viewTmsBillDetails';
  return request(`/server/api/tms/${url}`, {
    method: 'POST',
    body,
  });
}

/**
 * 移除账单
 */
export async function removeBill(params) {
  return request(`/server/api/tms/tms-bill/deleteTmsBill`, {
    method: 'POST',
    body: params,
  });
}
// 提交审核
export async function commitBill(params) {
  return request(`/server/api/tms/tms-bill/commitTmsBill`, {
    method: 'POST',
    body: params,
  });
}

/**
 * 查询帐单明细
 */
export async function selectBillInfo(params) {
  const { type, open, ...body } = params;
  const url = open ? 'open' :  'tms-bill'
  
  return request(`/server/api/tms/${url}/${getBillInfoUrl(type,'list')}`, {
    method: 'POST',
    body,
  });
}

/**
 * 查询附件
 */
export async function selectFileList(params) {
  return request(`/server/api/tms/attachment/selectFileList`, {
    method: 'POST',
    body: params,
  });
}

/**
 * 保存基本信息
 */
export async function billOperate(params) {
  let url = '';
  const { flag, ...body } = params;
  if (flag === 0) {
    url = body.id ? 'saveTmsBillDraft' : 'insertSaveTmsBillDraft';
  } else {
    url = body.id ? 'saveTmsBillDraftAndCommit' : 'insertSaveTmsBillDraftAndCommit';
  }
  return request(`/server/api/tms/tms-bill/${url}`, {
    method: 'POST',
    body,
  });
}

/**
 * 移除账单明细
 */
export async function removeBillInfo(params) {
  return request(`/server/api/tms/tms-bill/removeTmsBillDetail`, {
    method: 'POST',
    body: params,
  });
}
//撤回
export async function recallTmsBill(params) {
  return request(`/server/api/tms/tms-bill/recallTmsBill`, {
    method: 'POST',
    body: params,
  });
}

/**
 *查询新增明细列表
 */
export async function seleceBillAddInfo(params) {
  const { type, open, ...body } = params;
  const url = open ? 'open' : (type==='MISCELLANEOUS'?'tms-miscellaneous': 'tms-bill');
  return request(`/server/api/tms/${url}/${getBillInfoUrl(type)}`, {
    method: 'POST',
    body,
  });
}
// 明细详情
export async function detailsInfo(params) {
  const { type, open, ...body } = params;
  const url = open ? 'open' : 'tms-bill';
  return request(`/server/api/tms/${url}/${getBillDetailsInfoUrl(type, open)}`, {
    method: 'POST',
    body,
  });
}

/**
 * 新增明细
 */
export async function addBillInfoDetail(params) {
  return request(`/server/api/tms/tms-bill/addBillDetail`, {
    method: 'POST',
    body: params,
  });
}

export async function selectBillTax(params) {
  return request(`/server/api/tms/tms-bill/selectBillDetail`, {
    method: 'POST',
    body: params,
  });
}

/**
 * 新增 编辑车辆
 */
export async function vehicleOperate(params) {
  const { id, ...body } = params;
  const url = id ? 'updateVehicle' : 'insertVehicle';
  return request(`/server/api/tms/tms-vehicle/${url}`, {
    method: 'POST',
    body: params,
  });
}

/**
 * 新增编辑 证件
 */
export async function infoOperate(params) {
  const { id, ...body } = params;
  const url = id ? 'updatePapers' : 'insertPapers';
  return request(`/server/api/tms/tms-vehicle-papers/${url}`, {
    method: 'POST',
    body: params,
  });
}

// 冲借款列表
export async function selectBorrowInfo(params) {
  const { id, open, ...body } = params;
  const url = open ? 'open/selectRushedList_s' : 'tms-bill/selectRushedList';
  return request(`/server/api/tms/${url}`, {
    method: 'POST',
    body,
  });
}

//油价统计列表：
export async function seleceBillOilInfo(params) {
  const { type, open, ...body } = params;
  const url = open
    ? 'open/selectOilPriceStatisticsList_s'
    : 'tms-oil-price-statistics/selectTmsOilPriceStatisticsList';
  return request(`/server/api/tms/${url}`, {
    method: 'POST',
    body,
  });
}

//车辆统计列表：
export async function seleceBillVehicleInfo(params) {
  const { type, open, ...body } = params;
  if (!type) return;
  const url = open ? 'open' : 'tms-vehicle-statistics';
  return request(`/server/api/tms/${url}/${getBillVehicleInfoUrl(type, open)}`, {
    method: 'POST',
    body,
  });
}

function getBillVehicleInfoUrl(type, open) {
  let url = '';
  switch (type) {
    case 'UPKEEP':
      url = open ? 'selectUpkeepStatisticsList_s' : 'selectTmsVehicleUpkeepStatisticsList';
      break;
    case 'ETCCHANGE':
      url = open ? 'selectEtcStatisticsList_s' : 'selectTmsVehicleEtcchangeStatisticsList';
      break;
    case 'INSURANCE':
      url = open ? 'selectInsuranceStatisticsList_s' : 'selectTmsVehicleInsuranceStatisticsList';
      break;
    case 'ANNUALAUDIT':
      url = open ? 'selectInspectionStatisticsList_s' : 'selectTmsVehicleAnnualauditStatisticsList';
      break;
    case 'OIL':
      url = open ? 'selectOilStatisticsList_s' : 'selectTmsVehicleOilStatisticsList';
      break;
  }
  return url;
}

function getBillInfoUrl(type,list) {
  let url = '';
  switch (type) {
    case 'UPKEEP':
      url = 'selectUpkeepList_s';
      break;
    case 'ETCCHANGE':
      url = 'selectTmsChangeEtc_s';
      break;
    case 'INSURANCE':
      url = 'selectInsuranceList_s';
      break;
    case 'ANNUALAUDIT':
      url = 'selectInspectionList_s';
      break;
    case 'OIL':
      url = 'selectOilList_s';
      break;
    case 'DRIVERPAPER':
      url = 'selectPapersList_s';
      break;
    case 'MISCELLANEOUS':
      url =list?'selectMiscellaneousList_s' :'selectNotBillList';
      break;
  }
  return url;
}

function getBillDetailsInfoUrl(type, open) {
  let url = '';
  switch (type) {
    case 'UPKEEP':
      url = open ? 'selectUpkeepView_s' : 'viewTmsUpkeep_s';
      break;
    case 'ETCCHANGE':
      url = open ? 'selectEtcChangeView_s' : 'viewTmsChangeEtc_s';
      break;
    case 'INSURANCE':
      url = open ? 'selectInsuranceView_s' : 'viewTmsInsurance_s';
      break;
    case 'ANNUALAUDIT':
      url = open ? 'selectInspectionView_s' : 'viewTmsInspection_s';
      break;
    case 'OIL':
      url = open ? 'selectOilView_s' : 'viewTmsOil_s';
      break;
    case 'DRIVERPAPER':
      url = open ? 'selectPapersView_s' : 'viewTmsPapers_s';
      break;
    case 'MISCELLANEOUS':
      url = open ? 'selectMiscellaneousView_s' : 'viewTmsMiscellaneous_s';
      break;
  }
  return url;
}

/**
 * 账单明细导出
 */
export async function detailInfoDownload(params) {
  params.token = localStorage.getItem('token');
  const url = `/server/api/tms/tms-bill/exportTmsBillDetails?${stringify(params)}`;
  window.open(url);
}
